#ifndef _CALIBRATION_H__
#define _CALIBRATION_H__

#include "global.h"



#ifndef uint16
typedef unsigned short uint16;
#endif

#ifndef int8
typedef char int8;
#endif

#ifndef int16
typedef short int16;
#endif

#define NV_MULTI_LANG_ID   (405)
#define GSM_CALI_ITEM_ID   (0x2)
#define GSM_IMEI_ITEM_ID   (0x5)
#define XTD_CALI_ITEM_ID   (0x516)
#define LTE_CALI_ITEM_ID   (0x9C4)
#define BT_ITEM_ID         (0x191)

#define BT_ADDR_LEN  6
typedef struct BT_CONFIG_T
{
	BYTE  bt_addr[BT_ADDR_LEN];
	WORD  xtal_dac;
}BT_CONFIG;


#if 0
const int NUM_TEMP_BANDS = 5;
const int NUM_RAMP_RANGES = 16;        //constant parameter numbers, 16 level
const int NUM_TX_LEVEL = 16;          // 2db per step
const int NUM_RAMP_POINTS = 20;
const int NUM_GSM_ARFCN_BANDS = 6;
const int NUM_DCS_ARFCN_BANDS = 8;
const int NUM_PCS_ARFCN_BANDS = 7;
const int NUM_GSM850_ARFCN_BANDS = 6;
const int MAX_COMPENSATE_POINT = 75;
#else
#define NUM_TEMP_BANDS (5)
#define NUM_RAMP_RANGES (16)       //constant parameter numbers, 16 level
#define NUM_TX_LEVEL (16)          // 2db per step
#define NUM_RAMP_POINTS (20)
#define NUM_GSM_ARFCN_BANDS   (6)
#define NUM_DCS_ARFCN_BANDS   (8)
#define NUM_PCS_ARFCN_BANDS  (7)
#define NUM_GSM850_ARFCN_BANDS   (6)
#define MAX_COMPENSATE_POINT   (75)
#endif



typedef struct 
{
	int16 stage0[NUM_RAMP_POINTS];
	int16 stage1[NUM_RAMP_POINTS];
	int16 stage2[NUM_RAMP_POINTS];
	int16 stage3[NUM_RAMP_POINTS];
	int16 stage4[NUM_RAMP_POINTS];
	int16 stage5[NUM_RAMP_POINTS];
	int16 stage6[NUM_RAMP_POINTS];
	int16 stage7[NUM_RAMP_POINTS];
	int16 stage8[NUM_RAMP_POINTS];
	int16 stage9[NUM_RAMP_POINTS];
	int16 stage10[NUM_RAMP_POINTS];
	int16 stage11[NUM_RAMP_POINTS];
	int16 stage12[NUM_RAMP_POINTS];
	int16 stage13[NUM_RAMP_POINTS];
	int16 stage14[NUM_RAMP_POINTS];
	int16 stage15[NUM_RAMP_POINTS];
	
}RF_ramp_table_T;

typedef struct
{
    // uint16 RF_ID;
    // uint16 RF_Project_ID;	
    uint16 slope_length ;
    uint16 cdac ;
    uint16 cafc ;
    uint16 slope_struct[3][17] ;
    
}RF_aero2_AFC_T ;

typedef struct
{
    /************* AFC  ********************/
    uint16 is_using_si4134;        // TRUE, the si4134 from Silicon lab is used. Then the way to calibrate
    // the 13Mhz is different. For si4134, Total 64 steps( 1.0 ppm per step)
    // can be set to set coarse 13Mhz.
    uint16 si4134_coarse_13m;      // For si4134, it is 6bits, and for setting the CDAC register of si4134
    
    uint16 afc_center;
    uint16 afc_slope_per_6bits;
    
}RF_aero1_AFC_T ; 
typedef struct
{
    /************* AFC  ********************/
    RF_aero1_AFC_T rf_aero1_afc_struct;
    // Oscillator temperature calibration table.
    int16  osci_temp_comp_in_dac[ NUM_TEMP_BANDS ];     // In Hz
    // Temp -30 ~ -11 
    // Temp -10 ~ 9 
    // Temp  10 ~ 29 
    // Temp  30 ~ 49 
    // Temp  50 ~ 69 
    RF_aero2_AFC_T rf_aero2_afc_struct;                                                        
} RF_param_common_DSP_use_T;

typedef struct
{
/*int16 RF_ramp_up_table_stage0[NUM_RAMP_POINTS]; //64 words totally
int16 RF_ramp_up_table_stage1[NUM_RAMP_POINTS];
int16 RF_ramp_down_table_stage0[NUM_RAMP_POINTS];
    int16 RF_ramp_down_table_stage1[NUM_RAMP_POINTS];*/
    int16 rf_ramp_param_constant_up[NUM_RAMP_RANGES];//const part of the ramp tale
    int16 rf_ramp_param_constant_down[NUM_RAMP_RANGES];//const part of the ramp tale
    int16 rf_ramp_up_param_num;//the number of ramp up parameters in the ramp parameters table for one level
    int16 rf_ramp_down_param_num;//the number of ramp down
	
	/* int16 temperature_and_voltage_compensate_structrue[30]*/
    int16 reserved[64 - 2*NUM_RAMP_RANGES - 1 - 1];//keep 64words totally to keep consistency with dsp code
	
    int16 TX_compensate_boundary_arfcn_index[4];
    int16 RF_ramppwr_step_factor[5][NUM_TX_LEVEL];
    int16 RF_ramp_delta_timing[NUM_TX_LEVEL];
    int16 RF_ramp_PA_power_on_duration[NUM_TX_LEVEL];
	//    int16 RX_compensate_boundary_arfcn_index[4];
	//    int16 RX_compensate_value[5];
    int16 reserved1[9];
    int16 temperature_RX_compensate_boundary_ADC[4];
    int16 temperature_RX_compensate_value[5];
    int16 temperature_TX_compensate_boundary_ADC[4];
    int16 temperature_TX_rampwr_step_factor[5];
    int16  ramp_switch_level;
    uint16 afc_alg_bias;
    uint16 agc_ctrl_word[91];   // The index is the expected received signal strangth in db in 2 db steps,
	// the content is the control word set the RF receiver chain agc.
    
    int16  max_rf_gain_index;
    int16  reserve_for_future[20];
    
    int16  RX_compensate_value[MAX_COMPENSATE_POINT];
    RF_ramp_table_T RF_ramp_table;
    
} RF_param_band_DSP_use_T;


typedef struct
{
    RF_param_common_DSP_use_T   rf_common_param_dsp_use;
    RF_param_band_DSP_use_T     rf_gsm_param_dsp_use;
    RF_param_band_DSP_use_T     rf_dcs_param_dsp_use;
    RF_param_band_DSP_use_T     rf_pcs_param_dsp_use;
    RF_param_band_DSP_use_T     rf_gsm850_param_dsp_use;	
}RF_param_DSP_use_T;

typedef struct
{
    int8    rx_whole_band_comp_gsm[NUM_GSM_ARFCN_BANDS];
    int8    rx_whole_band_comp_dcs[NUM_DCS_ARFCN_BANDS];
    int8    rx_whole_band_comp_pcs[NUM_PCS_ARFCN_BANDS];
    int8    rx_whole_band_comp_gsm850[NUM_GSM850_ARFCN_BANDS];        
    int8    pa_GSM_temp_comp[ NUM_TEMP_BANDS];
    
    int8    pa_DCS_temp_comp[ NUM_TEMP_BANDS];
    
    int8    pa_PCS_temp_comp[ NUM_TEMP_BANDS];
    int8    pa_GSM850_temp_comp[ NUM_TEMP_BANDS];
    
    /******************** RX level calculation *********************/
    // The received signal level(RSSI) value reported to the network may need to be adjusted based on the received signal
    // level.
    int8    rxlev_GSM_temp_comp[ NUM_TEMP_BANDS];
    
    int8    rxlev_DCS_temp_comp[ NUM_TEMP_BANDS];
    
    int8    rxlev_PCS_temp_comp[ NUM_TEMP_BANDS];
    int8    rxlev_GSM850_temp_comp[ NUM_TEMP_BANDS];
    
    /****** PA ramp compensation for battery voltage variation. *******/
    int8    pa_GSM_vcc_comp_threshold;      // If the voltage is below the threshold, pa compensation is needed. Battery measure ADC value.
    int8    pa_GSM_vcc_comp_step;           // The PA ramp level need to be changed per threshold.
    int8    pa_DCS_vcc_comp_threshold;      // If the voltage is below the threshold, pa compensation is needed.
    int8    pa_DCS_vcc_comp_step;           // The PA ramp level need to be changed per threshold.
    int8    pa_PCS_vcc_comp_threshold;      // If the voltage is below the threshold, pa compensation is needed.
    int8    pa_PCS_vcc_comp_step;           // The PA ramp level need to be changed per threshold.
    int8    pa_GSM850_vcc_comp_threshold;      // If the voltage is below the threshold, pa compensation is needed. Battery measure ADC value.
    int8    pa_GSM850_vcc_comp_step;           // The PA ramp level need to be changed per threshold.
    
} RF_param_MCU_use_T;

typedef struct
{
    /**************** Battery monitor *********************/
    // Battery measurement calibration.
    // Due to the unprecise of the divider resistor value for measure the battery voltage, the voltage
    // value measured may be not accurate enough for battery charging.
    uint32 batt_mV_per_4bits;   // The actual voltage for 4bits ADC output.
    
    /******************* 32K/13M calibration. ***************/
    /**** the ratio of 32k and 13Mhz needs to be calibrated running ***/
    /***  for deep sleep mode ****/
    
    uint16  clock_32k_cal_interval; // How often the calibration shall be invoked.
    uint16  clock_32k_cal_duration; // How long the calibration shall be done.
}Misc_cal_MCU_use_T;

typedef struct
{
    uint32    adc[2];           // calibration of ADC, two test point
    uint32    battery[2];       // calibraton of battery(include resistance), two test point
    uint32    reserved[8];      // reserved for feature use.
}ADC_T;

typedef struct
{
    uint16              calibration_param_version;
    RF_param_DSP_use_T  rf_param_dsp_use;
    RF_param_MCU_use_T  rf_param_mcu_use;
    Misc_cal_MCU_use_T  misc_cal_mcu_use;
    int16                   mic_gain;        // In DB
    int16                   auxi_mic_gain;   // In DB
    uint16                  PM_version;      // the version of phone module
    
    // Following Added by Lin.liu. (2003-08-29), CR: MS4474
    uint16                  software_version;   // Software version of Tools
    uint16                  station_num;
    uint8                   operate_code[ 16 ];
    uint32                  date;
    
    ADC_T                   adc;             // Added By Lin.liu, for ADC calibration
    // End Lin.liu Added.
    
}calibration_param_T;


char* GetErrorDesc( UINT dwID  );

DWORD GSMCaliPreserve( BYTE* lpCode, DWORD dwCodeSize, 
                       BYTE* lpReadBuffer, DWORD dwReadSize,
                       BOOL bOldReplaceNew , BOOL bContinue );

BOOL  XFindNVOffsetEx(WORD wId,BYTE* lpCode,DWORD dwCodeSize,
                      DWORD* dwOffset,DWORD* dwLength,BOOL *bBigEndian, BOOL bModule);

BOOL  XCheckNVStructEx(BYTE* lpPhoBuf, DWORD dwPhoSize,BOOL *bBigEndian,BOOL bModule);

DWORD  XPreserveIMEIs( int *pIMEIID, UINT nIMEICount, BYTE* lpCode, DWORD dwCodeSize, 
					  BYTE* lpReadBuffer, DWORD dwReadSize, int *nFailedIMEIIndex,
					  BOOL bOldReplaceNew, BOOL bContinue);
DWORD XPreserveNVItem(  WORD wID, BYTE* lpCode, DWORD dwCodeSize, 
					  BYTE* lpReadBuffer, DWORD dwReadSize,
					  BOOL bOldReplaceNew/*=FALSE*/, BOOL bContinue/*=FALSE*/ );

#endif

